package com.example.softwareengg;



import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Serializable;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.concurrent.TimeoutException;

import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
@SuppressWarnings("serial")

public class ScreenA extends Activity implements OnClickListener,Serializable {
	//take ip from settings screen.
	public TextView result;
	public StringBuffer status =new StringBuffer("Not Connected");
	
	String serverIP=MyOverridenApp.getserverIP();
	static PrintWriter put=null;
	int flag = 0;
	//ProgressDresult.setText("connected");ialog progressDialog ;
	static String password1;
	static String shruti;
	EditText username ;
	EditText password ;
	String serverReply = null;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		 setContentView(R.layout.screena);
		 result = (TextView)findViewById(R.id.textviewresult);
		 //remove this
		 result.setText("connected");
	        
        username = (EditText) findViewById(R.id.username);
    	password = (EditText) findViewById(R.id.password);
    	
        
		Button login = (Button) findViewById(R.id.login);
		Button settings = (Button)findViewById(R.id.settings);
		
		
    	
		login.setOnClickListener(this);
		settings.setOnClickListener(this);
		
		
		
	}
	
	 public void onClick(View v)  {
	        switch (v.getId()) {
	        case R.id.settings:
	        {
				   Intent intent = new Intent(ScreenA.this,ScreenSettings.class);
				    startActivity(intent);
				    break;

	        }
	       	        case R.id.login:
	        {
	        	//LinearLayout linlaHeaderProgress = (LinearLayout) findViewById(R.id.linlaHeaderProgress);
	        	//linlaHeaderProgress.setVisibility(View.VISIBLE);
	        	serverIP=MyOverridenApp.getserverIP();
	        	if(serverIP.trim().equals(""))
	        	
	        	Toast.makeText(getApplicationContext(),"Server Id not added\n", Toast.LENGTH_LONG).show();
	        	
	        	
	        	else
	        	{
	        	password1 = password.getText().toString().trim();
	        	shruti = username.getText().toString().trim();
	    			
	        	MyOverridenApp.setUser(shruti);
	        	if(!(serverIP.trim().equals("")))
	        	{
	        	try {
					Socket client1 = new Socket(serverIP, 8000);
					MyOverridenApp.setClient(client1);
					//client1.setSoTimeout(5000);
					
					DataOutputStream outToServer = new DataOutputStream(client1.getOutputStream());
					//Toast.makeText(getApplicationContext(),username+password1+"asgjhg", Toast.LENGTH_LONG).show();
					outToServer.writeBytes("login "+shruti+" "+password1+"\n");
					BufferedReader inFromServer = new BufferedReader(new InputStreamReader(client1.getInputStream()));
					
						serverReply = inFromServer.readLine();					
						//progressDialog.dismiss();
						
					if(serverReply.trim().equalsIgnoreCase("true"))
						flag = 1;
					
					
					
					//Toast.makeText(getApplicationContext(),"flag = "+flag, Toast.LENGTH_LONG).show();
					
					//linlaHeaderProgress.setVisibility(View.GONE);
					
				} catch (UnknownHostException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	        	
	        	if( shruti.trim().equals(""))
	    		 {    
	    		   username.setError( "Username is required!" );
	    		   //You can Toast a message here that the Username is Empty
	    		 }
	    		else if( password1.trim().equals(""))
	    		 {    
	    		   password.setError( "Enter password" );
	    		   //You can Toast a message here that the Username is Empty
	    		 }
	        	
	    		if(flag == 1)
	    		{
	    		status = new StringBuffer("connected");
	    		Intent i = new Intent(ScreenA.this, ScreenB.class);
	    		
	    		
	    		
	    		
	    			
	    		
	    		 result.setText(status);
	    		   startActivity(i);
	    		
	    		
	    		}
	    		
	    		else 
	    		{
	    			
		    		 result.setText("connected");
		    		 	
	    			Toast.makeText(getApplicationContext(),"Error Logging in!", Toast.LENGTH_LONG).show();
	    			
	    		}
	    		
	        
	        	}}
	        	
	        	
	        	break;}
	        	        default:
	            break;
	        	}
	        }
	 
	 @Override
	 public void onBackPressed() {
	 }
	       

	

	 }
